<?php
    $mdModule = new Application_Model_DbTable_Module();
    $mdController = new Application_Model_DbTable_Controller();
    $mdAction = new Application_Model_DbTable_Action();
    $modules = $mdModule->fetchAll();
?>
<div class="grid_12">
<?php foreach($modules as $module){?>
    <!-- Module -->
    <div class="box round first fullpage">
        <h2>Module "<?=$module->display_name?>" </h2>
        <div class="block" style="position: relative;">
            <div class="message warning loading">
                <p>Loading...</p>
            </div>
            
            <a href="<?=$this->url(array("module"=>"admin","controller"=>"webconfig","action"=>"add-ctrl","idmd"=>$module->idmodules), null, true)?>" class="btn btn-black" style="margin-bottom: 10px;">Add Controller</a>
            <div class="dataTables_wrapper">
                <table class="data display datatable">
                    <thead>
                        <tr>
                            <th width="50" class="center">STT</th>                            
                            <th>Tên</th>
                            <th width="60" class="center">Sửa</th>
                            <th width="60" class="center">Xóa</th>
                            <th width="100" class="center">Thêm action</th>
                        </tr>
                    </thead>
                    
                    <tbody>
                    <?php
                    $controllers = $mdController->fetchAll("modules_id='$module->idmodules'");
                    $stt=1;
                    foreach($controllers as $controller)
                    {  $parity = ($stt%2)?"odd":"even";?>
                        <tr class="gradeA <?=$parity?>">
                            <td class="center"><?=$stt?></td><?php $stt++;?>
                            <td><?=$controller->display_name?></td>
                            <td class="center">
                                <a href="<?=$this->url(array("module"=>"admin","controller"=>"webconfig","action"=>"edit-ctrl","id"=>$controller->idcontrollers), null, true)?>">
                                    <img src="<?=$this->baseUrl("templates/admin/img/icon-edit.png")?>" alt="[Sửa]" title="Sửa"/>
                                </a>
                            </td>
                            <td class="center">
                                <a class="del-ctrl" href="<?=$this->url(array("module"=>"admin","controller"=>"webconfig","action"=>"del-ctrl","id"=>$controller->idcontrollers), null, true)?>">
                                    <img src="<?=$this->baseUrl("templates/admin/img/icon-del.png")?>" alt="[Xóa]" title="Xóa"/>
                                </a>
                            </td>
                            <td class="center">
                                <a style="color: blue;" href="<?=$this->url(array("module"=>"admin","controller"=>"webconfig","action"=>"add-act","idctrl"=>$controller->idcontrollers), null, true)?>">[+]</a>
                            </td>
                        </tr>
                        <?php
                        $select = $mdAction->select();
                        $select->where("controllers_id='$controller->idcontrollers'")
                               ->order("idactions");
                        $actions = $mdAction->fetchAll($select);
                        foreach($actions as $action)
                        {?>
                            <tr class="gradeA <?=$parity?>">
                            <td class="center"><?=$stt?></td><?php $stt++;?>
                            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                |_____<?=$action->display_name?>
                            </td>
                            <td class="center">
                                <a href="<?=$this->url(array("module"=>"admin","controller"=>"webconfig","action"=>"edit-act","id"=>$action->idactions), null, true)?>">
                                    <img src="<?=$this->baseUrl("templates/admin/img/icon-edit.png")?>" alt="[Sửa]" title="Sửa"/>
                                </a>
                            </td>
                            <td class="center">
                                <a class="del-action" href="<?=$this->url(array("module"=>"admin","controller"=>"webconfig","action"=>"del-act","id"=>$action->idactions), null, true)?>">
                                    <img src="<?=$this->baseUrl("templates/admin/img/icon-del.png")?>" alt="[Xóa]" title="Xóa"/>
                                </a>
                            </td>
                            <td class="center">
                            </td>
                        </tr>
                        <?php }?>
                    <?php }?>
                    </tbody>
                </table>
            </div>       
        </div>
    </div>
<?php }?>
</div>
<div class="dialog-message" title="Thông báo" style="display: none;"></div>

<script>
$(function(){
    $(".del-ctrl").click(function(){
        var link = $(this).attr("href");
        $(".dialog-message").html("<div class='message warning'><h5>Cảnh báo</h5><p>Xóa <b>controller</b> sẽ xóa tất cả các <b>action</b> thuộc nó. Bạn có muốn xóa hay không.</p></div>").dialog({
            autoOpen: true,
            modal: true,
            buttons: {
                "Xóa": function(){
                    window.location = link;
                },
                "Không xóa": function(){
                    $(this).dialog("close");
                }
            }
        });
        return false;
    });
    
    $(".del-action").click(function(){
        var link = $(this).attr("href");
        $(".dialog-message").html("<div class='message warning'><h5>Cảnh báo</h5><p>Bạn có chắc muốn xóa <b>action</b> hay không.</p></div>").dialog({
            autoOpen: true,
            modal: true,
            buttons: {
                "Xóa": function(){
                    window.location = link;
                },
                "Không xóa": function(){
                    $(this).dialog("close");
                }
            }
        });
        return false;
    });
})
</script>